Binary File MCQ Question Bank For Class 12


1. Out of the followings which mode is used for both reading and writing in binary format in file?


Option a.) wb
Option b.) wb+
Option c.) w
Option d.) w+

Answer

#Answer b) wb+






2. Which of the following is not true about binary files?


Option a.) Binary files are store in terms of bytes
Option b.) When you open binary file in text editor will show garbage values
Option c.) Binary files represent ASCII value of characters
Option d.) All of the above

Answer

#Answer c) Binary files represent ASCII value of characters






3. What is the difference between wb and wb+ mode?


Option a.) wb mode is used to open binary file in write mode and wb+ mode open binary file both for read and write operation.
Option b.) In wb mode file open in write mode and wb+ in read mode
Option c.) File pointer is at beginning of file in wb mode and in wb+ at the end of file
Option d.) No difference

Answer

#Answer a) wb mode is used to open binary file in write mode and wb+ mode open binary file both for read and write operation.





4. The pickle module in Python is used for:


Option a.) Serializing any Python object structure
Option b.) De-serializing Python object structure
Option c.) Both a and b
Option d.) None of these

Answer

#Answer c) Both a and b






5. Which method is used to convert Python objects for writing data in binary file?


Option a.) write()
Option b.) load()
Option c.) store()
Option d.) dump()

Answer

#Answer d) dump()






6. seek() function is used for _______.


Option a.) positions the file object at the specified location.
Option b.) It returns the current position of the file object
Option c.) It writes the data in binary file
Option d.) None of these

Answer

#Answer a) positions the file object at the specified location.






7. Which is not the valid mode for binary files?


Option a.) r
Option b.) rb
Option c.) wb
Option d.) wb+

Answer

#Answer a) r






8. Which of the following function is used to read the data in binary file?


Option a.) read()
Option b.) open()
Option c.) dump()
Option d.) load()

Answer

#Answer d) load()






9. Suresh wants to open the binary file student.dat in read mode. He writes the following statement but he does not know the mode. Help him to find the same.
F=open(‘student.dat’, ____)


Option a.) r
Option b.) rb
Option c.) w
Option d.) wb

Answer

#Answer b) rb






10. This method returns an integer that specifies the current position of the file object.


Option a.) seek()
Option b.) load()
Option c.) position()
Option d.) tell()

Answer

#Answer d) tell()






11. What is pickling?


Option a.) It is the process to read binary file
Option b.) It is the process to position the file pointer
Option c.) It is a process by which a Python object is converted to a byte stream
Option d.) None of these

Answer

#Answer c) It is a process by which a Python object is converted to a byte stream







Case Study Based Question-1

Mr. Zack Sullivan loves programming. He joined an institute for learning. He is learning python. He learned all the python concepts like strings, lists, tuple , dictionaries etc. but he wants to learn file handling in python. He is trying to learn binary file handling. His teacher gave him partial code to write and read data from employee.dat having structure empno, name, salary. Help Zack to complete the code:

___________________ # statement 1
def addrecords():
fw= _____________ #statement 2
dict={}
ch=’y’
while ch==’y’:
eno=int(input(“enter employee number”))
nm= input(“enter employee name”)
sal=int(input(“enter employee salary”))
dict={‘empno’:eno,’name’:nm,’salary’:sal}
____________________ # statement 3
ch=input(“add more record”)
fw.close()

# function to diplay records
def display():
dict={}
fr= _____________ # statement 4
dict=____________ # statement 5
fr.close()
print(“data :”,dict)


Answer

#Answer questions 12-16 based on above case study





12. Help Zack to import the module to perform binary file operation in statement 1.


Option a.) csv
Option b.) random
Option c.) pickle
Option d.) file

Answer

#Answer c) pickle






13. Which statement is used from the following for statement 2 to open the binary file in write mode?


Option a.) open(“employee.dat”,’w’)
Option b.) open(“employee.dat”,’wb’)
Option c.) open(“employee.dat”,’w+’)
Option d.) open(“employee.dat”,’r’)


Answer

#Answer b) open(“employee.dat”,’wb’)






14. Which statement is used from the following for statement 3 to write dictionary data created in above code, namely dict, is written in binary file employee.dat file?


Option a.) pickle.dump(dict,fw)
Option b.) pickle.write(dict,fw)
Option c.) pickle.save(dict,fw)
Option d.) pickle.store(dict)


Answer

#Answer a) pickle.dump(dict,fw)






15. Which statement is used from the following for statement 4 to open the binary file in read mode?


Option a.) open(“employee.dat”,’r’)
Option b.) open(“employee.dat”,’r+’)
Option c.) open(“employee.dat”,’a’)
Option d.) open(“employee.dat”,’rb’)

Answer

#Answer d) open(“employee.dat”,’rb’)






16. Compelete statement 5 to read data in dictionary namely dict from the opened binary file?


Option a.) dict=pk.read(fr)
Option b.) dict=pickle.load(fr)
Option c.) pickle.load(dict,fr)
Option d.) none of these

Answer

#Answer b) dict=pickle.load(fr)







Case Study Based Question-2

Now Mr. Zack has given the following code to modify the records of employees from employee.dat used in above code. He has to increase Rs. 2000 in the salary of those who are getting less than 15000. Mr. Zack has to find the records and change the salary in place. His teacher gave him partial code. Help him to complete the code.

import pickle as pk
found=False
emp={}
fin = ___________ #1 statement : open file both in read write mode
# read from file
try:
while true:
pos= _______ #2 store file pointer position before reading record
emp=_______ #3 to read the record in emp dictionary
if emp[‘salary’]<15000:
emp[‘salary’]+=10000
_________ #4 place file pointer at exact location of record
pickle.dump(emp,fin)
found=True
except EOFError:
if found==False:
print(“record not found”)
else:
print(“successfully updated”)
fin.close()



17. In #1 statement open the file in read and write mode. Which statement is used out of the followings?


Option a.) open(“employee.dat”,’rb+’)
Option b.) open(“employee.dat”,’r+’)
Option c.) open(“employee.dat”,’a’)
Option d.) open(“employee.dat”,’rb’)

Answer

#Answer a) open(“employee.dat”,’rb+’)






18. Choose the appropriate statement to complete #2 statement to store file pointer position before reading record.


Option a.) pk.seek(pos)
Option b.) fin.tell()
Option c.) pk.position()
Option d.) pk.tell()

Answer

#Answer b) fin.tell()







19. Choose the appropriate statement to complete #3 statement to read record in emp dictionary.


Option a.) pk.read(fin)
Option b.) pickle.load(fin,emp)
Option c.) pk.dump(emp)
Option d.) pk.load(fin)

Answer

#Answer d) pk.load(fin)






20. Choose the appropriate statement to complete #4 statement to place file pointer at exact location of record


Option a.) fin.seek(pos)
Option b.) pos=fin.seek()
Option c.) fin.position()
Option d.) none of the above

Answer

#Answer b) pos=fin.seek().